package com.lanou.sfu.songforu.music_library_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.MusicLibraryAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.view.MyEvent;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 15/10/20.
 */
public class MusicLibraryFragment extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private View v;
    private MusicLibraryAdapter mAdapter;
    private  String message;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_music_library, null);
        mTabLayout = (TabLayout) v.findViewById(R.id.music_library_tabLayout);
        mViewPager = (ViewPager) v.findViewById(R.id.music_library_viewPager);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mAdapter = new MusicLibraryAdapter(getChildFragmentManager());

        mViewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
        mTabLayout.setBackgroundColor(0xbbbbbb);
        mTabLayout.setDrawingCacheBackgroundColor(0xcccccc);
        mViewPager.setOffscreenPageLimit(4);
    }

    public void onEvent(MyEvent.More more){
//        Log.i("xxx", more.getMessage());
//        message = more.getMessage();
        Log.i("xxx", more.getMessage() + "");
        switch (more.getMessage()) {
            case 1:
                mViewPager.setCurrentItem(2);
                break;
            case 2:
                mViewPager.setCurrentItem(3);
                break;
        }
//        if (message.equals("one")){
//            mViewPager.setCurrentItem(2);
//        }else if (message.equals("two")){
//            mViewPager.setCurrentItem(3);
//        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
